﻿namespace mncreator
{
    using System;
    using System.Threading;
    using System.Windows.Forms;
    using System.Windows.Automation;
    using System.Windows.Automation.Text;
    using System.Diagnostics;

    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            System.Drawing.Point prev = new System.Drawing.Point();

            do
            {
                System.Drawing.Point mouse = Cursor.Position;
                if (prev != mouse)
                {
                    prev = mouse;

                    System.Windows.Point pt = new System.Windows.Point(mouse.X, mouse.Y);
                    AutomationElement element = AutomationElement.FromPoint(pt);
                    if (element != null)
                    {
                        object pattern;
                        bool curPatOk = element.TryGetCurrentPattern(ValuePattern.Pattern, out pattern);

                        ValuePattern valuePattern = (curPatOk ? (ValuePattern)pattern : null);
                        string text = (null != valuePattern ? valuePattern.Current.Value : "NULL");

                        AutomationElement.AutomationElementInformation info = element.Current;

                        Trace.WriteLine(String.Format("undermice: [{0} {1}] {2} :: {3} `{4}`"
                            , pt.X.ToString("0000"), pt.Y.ToString("0000")
                            , info.ClassName, info.ControlType.ProgrammaticName
                            , (text.Length > 16 ? (text.Substring(0, 16) + "...") : text)
                            ));
                    }
                }

                Thread.Sleep(100);

                //Console.WriteLine("Element at position " + mouse + " is '" + element.Current.Name + "'");
                //
                //object pattern;
                //// the "Value" pattern is supported by many application (including IE & FF)
                //if (element.TryGetCurrentPattern(ValuePattern.Pattern, out pattern))
                //{
                //    ValuePattern valuePattern = (ValuePattern)pattern;
                //    Console.WriteLine(" Value=" + valuePattern.Current.Value);
                //}
                //
                //// the "Text" pattern is supported by some applications (including Notepad)and returns the current selection for example
                //if (element.TryGetCurrentPattern(TextPattern.Pattern, out pattern))
                //{
                //    TextPattern textPattern = (TextPattern)pattern;
                //    foreach (TextPatternRange range in textPattern.GetSelection())
                //    {
                //        Console.WriteLine(" SelectionRange=" + range.GetText(-1));
                //    }
                //}
                //Thread.Sleep(1000);
                //Console.WriteLine(); Console.WriteLine();
            }
            while (true);
        }
    }
}
